
package org.apache.solr.search;

import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.search.Query;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.request.SolrQueryRequest;

/**
 * Create a nested query, with the ability of that query to redefine it's type via
 * local parameters.  This is useful in specifying defaults in configuration and
 * letting clients indirectly reference them.
 * <br>Example: <code>{!query defType=func v=$q1}</code>
 * <br> if the q1 parameter is <code>price</code> then the query would be a function query on the price field.
 * <br> if the q1 parameter is <code>{!lucene}inStock:true</code> then a term query is
 *     created from the lucene syntax string that matches documents with inStock=true.
 */
public class NestedQParserPlugin extends QParserPlugin {

    public static String NAME = "query";

    @Override
    public void init(NamedList args) {
    }

    @Override
    public QParser createParser(String qstr, SolrParams localParams, SolrParams params, SolrQueryRequest req) {
        return new QParser(qstr, localParams, params, req) {
            QParser baseParser;
            ValueSource vs;
            String b;

            @Override
            public Query parse() throws ParseException {
                baseParser = subQuery(localParams.get(QueryParsing.V), null);
                return baseParser.getQuery();
            }

            @Override
            public String[] getDefaultHighlightFields() {
                return baseParser.getDefaultHighlightFields();
            }

            @Override
            public Query getHighlightQuery() throws ParseException {
                return baseParser.getHighlightQuery();
            }

            @Override
            public void addDebugInfo(NamedList<Object> debugInfo) {
                // encapsulate base debug info in a sub-list?
                baseParser.addDebugInfo(debugInfo);
            }
        };
    }
}